package com.cqtczh.qhd.driver.model;

import java.io.IOException;

import com.cqtczh.qhd.driver.R;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

/**
 * * @author 作者 陈修园:
 * 
 * @date 创建时间：2015-4-27 下午2:49:34
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class PushSoundService extends Service {

	private String tag = "PushSoundService";

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.v(tag, "音乐");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.v(tag, "已经启动");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		MediaPlayer mp = MediaPlayer.create(PushSoundService.this, R.raw.mssage);
		PowerManager pm=(PowerManager) getSystemService(PushSoundService.POWER_SERVICE);  
		 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
		 wl.acquire(); 
		if (mp!=null) {
			mp.stop();
			Log.v("mp", "不是空");
		}
		AudioManager audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
		int maxVolume =audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		Log.v("maxVolume", ""+maxVolume);
		maxVolume=maxVolume-3;
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);
		// mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
		// mp.prepareAsync();
		// mp.setVolume(0.5f, 0.5f);
		//mp.setd	
		 try {
			 Log.v(tag, "prepare");
			mp.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		 Log.v(tag, "start");
		mp.start();
		Log.v(tag, "音乐启动");
//		
//		SoundPool soundPool=new SoundPool(2, AudioManager.STREAM_MUSIC, 5);
//		soundPool.load(this,R.raw.mssage ,1);
//		soundPool.play(1, 1, 1, 0, 0, 1);
		 wl.release();  
		
		return super.onStartCommand(intent, flags, startId);
	}

}
